﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="BindableViewBinding.cs" company="">
//   
// </copyright>
// <summary>
//   The bindable view binding.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Libium.Bindings
{
    using Libium.UseCases;
    using Libium.Views;

    /// <summary>
    /// The bindable view binding.
    /// </summary>
    /// <typeparam name="T1">
    /// </typeparam>
    /// <typeparam name="T2">
    /// </typeparam>
    public abstract class BindableViewBinding<T1, T2> : Binding<T1, T2>
        where T1 : IViewBindable where T2 : IView
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="BindableViewBinding{T1,T2}"/> class.
        /// </summary>
        /// <param name="service">
        /// The service. 
        /// </param>
        /// <param name="useCase">
        /// The use case. 
        /// </param>
        /// <param name="view">
        /// The view. 
        /// </param>
        protected BindableViewBinding(IBindService service, T1 useCase, T2 view)
            : base(service, useCase, view)
        {
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///   Gets the use case.
        /// </summary>
        public T1 UseCase
        {
            get
            {
                return Object1;
            }
        }

        /// <summary>
        ///   Gets the view.
        /// </summary>
        public T2 View
        {
            get
            {
                return Object2;
            }
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        ///   The bind.
        /// </summary>
        public override void Bind()
        {
            View.Clear();
            View.Enabled = UseCase.Enabled;

            UseCase.EnabledChanged += OnElementEnabledChanged;
        }

        /// <summary>
        ///   The unbind.
        /// </summary>
        public override void Unbind()
        {
            View.Clear();
            View.Enabled = false;

            UseCase.EnabledChanged -= OnElementEnabledChanged;
        }

        #endregion

        #region Methods

        /// <summary>
        /// The on element enabled changed.
        /// </summary>
        /// <param name="useCase">
        /// The use case. 
        /// </param>
        private void OnElementEnabledChanged(IViewBindable useCase)
        {
            View.Enabled = UseCase.Enabled;
        }

        #endregion
    }
}